/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * An absolute panel positions all of its children absolutely, allowing them to
 * overlap as well.
 */
public class AbsolutePanel extends ComplexPanel {

  /**
   * Creates an empty absolute panel.
   */
  public AbsolutePanel() {
    setElement(DOM.createDiv());
    DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  }

  public boolean add(Widget w) {
    super.add(w);
    DOM.appendChild(getElement(), w.getElement());
    return true;
  }

  /**
   * Adds a widget to the panel at the specified position.
   * 
   * @param w the widget to be added
   * @param left the widget's left position
   * @param top the widget's top position
   * @return <code>true</code>
   */
  public boolean add(Widget w, int left, int top) {
    add(w);
    setWidgetPosition(w, left, top);
    return true;
  }

  public boolean remove(Widget w) {
    if (super.remove(w)) {
      DOM.removeChild(getElement(), w.getElement());
    }
    return true;
  }

  /**
   * Sets the position of the specified child widget.
   * 
   * @param w the child widget to be positioned
   * @param left the widget's left position
   * @param top the widget's top position
   */
  public void setWidgetPosition(Widget w, int left, int top) {
    Element h = w.getElement();
    if ((left == -1) && (top == -1)) {
      DOM.setStyleAttribute(h, "left", "");
      DOM.setStyleAttribute(h, "top", "");
      DOM.setStyleAttribute(h, "position", "static");
    } else {
      DOM.setStyleAttribute(h, "position", "absolute");
      DOM.setIntStyleAttribute(h, "left", left);
      DOM.setIntStyleAttribute(h, "top", top);
    }
  }

}
